/* =========================================================
 * treeGridHelper.js v1.0.0
 * =========================================================
 * 使用简介：
 * 基于easyui的treegrid:
 * 
onClickRow : function(row) {
    var $row = $("input[type=checkbox][name=id][value=" + row.id + "]")[0];
    $row.checked = !$row.checked;
    nodeClick(DOM, $row);
}

   onLoadSuccess : function(row, data) {
        uncheckAll();
        $("input[type=checkbox][name=id]").click(function() {
            nodeClick(DOM, this);
        });
        checkedMenu(data.rows);
    }
 * =========================================================
 *修改记录：
 * ========================================================= */


/* 获取子孙节点Id */
function getChildrenIds(DOM, nodeId) {
     var s = "";
     var node;
     try {
         node = DOM.treegrid("find", nodeId);
         if (!node) {
             node = DOM.treegrid("find", nodeId);
         }
     } catch (e) {
         node = DOM.treegrid("find", nodeId);
     }
     if (node && node.children && node.children.length > 0) {
         for (var i = 0; i < node.children.length; i++) {
             if (s === "") {
                 s = node.children[i].id;

             } else {
                 s = s + "," + node.children[i].id;
             }
             var sChilds = getChildrenIds(DOM, node.children[i].id);
             if (sChilds !== "") {
                 s = s + "," + sChilds;
             }
         }
     }
     return s;
 }

/* 节点选择事件 */
function nodeClick(DOM, thisObj) {
    var mId = thisObj.value.substr();

    var isChecked = thisObj.checked;
    var childIds = getChildrenIds(DOM, mId);

    if (typeof (childIds) === "string" && childIds !== "") {
        var childList = childIds.split(",");
        for (var i = 0; i < childList.length; i++) {
            if (isChecked)
                $("input[type=checkbox][name=id][value=" + childList[i] + "]")[0].checked = true;
            else
                $("input[type=checkbox][name=id][value=" + childList[i] + "]")[0].checked = false;
        }
    }
    var parentIds = getParentIds(DOM, mId);

    if (isChecked && typeof (parentIds) === "string" && parentIds !== "") {
        var parendList = parentIds.split(",");
        for (var m = 0; m < parendList.length; m++) {
            $("input[type=checkbox][name=id][value=" + parendList[m] + "]")[0].checked = true;
        }
    }

    if (!isChecked && typeof (parentIds) === "string" && parentIds !== "") {
        var parendListn = parentIds.split(",");
        for (var n = 0; n < parendListn.length; n++) {
            var pid = parendListn[n];
            var chkCount = getChildCheckedCount(DOM, pid);
            if (chkCount === 0) {
                $("input[type=checkbox][name=id][value=" + pid + "]")[0].checked = false;
            }
        }
    }
}
 
/* 获取子孙节点Id */
function getChildrenIds(DOM, nodeId) {
    var s = "";
    var node;
    try {
        node = DOM.treegrid("find", nodeId);
        if (!node) {
            node = DOM.treegrid("find", nodeId);
        }
    } catch (e) {
        node = DOM.treegrid("find", nodeId);
    }
    if (node && node.children && node.children.length > 0) {
        for (var i = 0; i < node.children.length; i++) {
            if (s === "") {
                s = node.children[i].id;

            } else {
                s = s + "," + node.children[i].id;
            }
            var sChilds = getChildrenIds(DOM, node.children[i].id);
            if (sChilds !== "") {
                s = s + "," + sChilds;
            }
        }
    }
    return s;
}

/* 获取上级ID */
function getParentIds(DOM, nodeId) {
    var s = "";
    var node;
    try {
        node = DOM.treegrid("find", nodeId);
        if (!node) {
            node = DOM.treegrid("find", nodeId);
        }
    } catch (e) {
        node = $(DOM).treegrid("find", nodeId);
    }
    if (node && node.parentId) {
        s = "" + node.parentId;
        var pid = getParentIds(DOM, node.parentId);
        if (pid !== "") {
            s = s + "," + pid;
        }
    }
    return s;
}

/* 获取下级选中数目 */
function getChildCheckedCount(DOM, nodeId) {
    var childs = getChildrenIds(DOM, nodeId);
    if (typeof (childs) !== "string" || childs === "") {
        return 0;
    }
    var chkCount = 0;
    var list = childs.split(",");
    for (var i = 0; i < list.length; i++) {
        if ($("input[type=checkbox][name=id][value=" + list[i] + "]:checked").size() > 0) {
            chkCount++;
        }
    }
    return chkCount;
}

function uncheckAll() {
    $("input[type=checkbox][name=id]").removeAttr("checked");
}

/* 选中具有权限的项 */
function checkedMenu(data) {
    for (var i = 0; i < data.length; i++) {
        var menuItem = data[i];
        if (menuItem.check) {
            $("input[type=checkbox][name=id][value=" + menuItem.id + "]")[0].checked = true;
        }
        if (menuItem.children && menuItem.children.length > 0) {
            checkedMenu(menuItem.children);
        }
    }
}
